import re

from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QHBoxLayout, QLabel

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from numpy import array, arctan2, min, max, sqrt, sin, cos, pi, uint8, seterr
from PIL import Image
import matplotlib.pyplot as plt


def plot_image(matrix, name):
	fig = plt.figure()
	ax = fig.add_subplot(111)
	ax.set_title(name)
	# 去掉坐标系和边界
	ax.set_axis_off()
	plt.subplots_adjust(left=0, right=1, top=1, bottom=0)

	ax.imshow(matrix, cmap='gray')
	# 将图表嵌入到FigureCanvasQTAgg 对象中
	canvas = FigureCanvas(fig)
	return canvas

class Target_view(QWidget):
	def __init__(self, dir1, dir2, dir3, SAVE_RESULT):
		super(Target_view, self).__init__()
		self.setWindowTitle("AOE and DOP")
		self.resize(660, 500)
		self.setWindowModality(Qt.WindowModal)
		self.setStyleSheet('background-color: #ffffff;')
		self.all_layout = QHBoxLayout()

		self.setLayout(self.all_layout)

		self.path1 = dir1
		self.path2 = dir2
		self.path3 = dir3
		self.save_manager = SAVE_RESULT
		self.setup_ui()

	def setup_ui(self):
		im1 = Image.open(self.path1).convert('L')
		im1 = array(im1)
		im2 = Image.open(self.path2).convert('L')
		im2 = array(im2)
		im3 = Image.open(self.path3).convert('L')
		im3 = array(im3)

		im1g = im1 / 255
		im2g = im2 / 255
		im3g = im3 / 255
		im1gm = (im1g - min(im1g)) / (max(im1g) - min(im1g))
		im2gm = (im2g - min(im2g)) / (max(im2g) - min(im2g))
		im3gm = (im3g - min(im3g)) / (max(im3g) - min(im3g))

		try:
			seterr(divide='ignore', invalid='ignore')
			aop = 0.5 * arctan2(((im2gm - im3g) / sin(2 * pi / 3)), ((2 * im1g - im2gm - im3g) / (1 - cos(2 * pi / 3))))
			x1 = (im3gm + im2gm) * sin(2 * aop) * sin(2 * pi / 3)
			x2 = (im2gm - im3gm) * cos(2 * aop) * cos(2 * pi / 3)
			dop = (im2gm - im3gm) / (x1 - x2)

			DEL_CHINESE = re.sub('[\u4e00-\u9fa5]', '', self.path1)
			NAME_FORWARD = re.sub("[A-Za-z./:，]", "", DEL_CHINESE)
			NAME_FORWARD = NAME_FORWARD.replace(" 0°", "")

			AOP_PATH = "./REASULT_POLARIZATION_VISION_IMAGE/" + NAME_FORWARD + "AOP.jpg"
			DOP_PATH = "./REASULT_POLARIZATION_VISION_IMAGE/" + NAME_FORWARD + "DOP.jpg"

			AOP_UINT = aop * 255.0
			DOP_UINT = dop * 255.0
			high, width = DOP_UINT.shape
			aop_array = Image.fromarray(uint8(AOP_UINT))
			dop_array = Image.fromarray(uint8(DOP_UINT))
			SAVE_MANAGER = ['不保存', '本机', '数据库', '本机和数据库']
			if self.save_manager == SAVE_MANAGER[1]:
				aop_array.save(AOP_PATH)
				dop_array.save(DOP_PATH)

			canvas_aop = plot_image(aop_array, "AOP")
			canvas_dop = plot_image(dop_array, "DOP")
			self.all_layout.addWidget(canvas_aop)
			self.all_layout.addWidget(canvas_dop)

		except RuntimeWarning as a:
			print("错误原因：", a)


if __name__ == '__main__':
	from sys import argv, exit
	app = QApplication(argv)

	path1 = r"D:\迅雷下载\偏振光\偏振光仿真和上位机\Image\2022-10-19 15-07-52 0°.bmp"
	path2 = r"D:\迅雷下载\偏振光\偏振光仿真和上位机\Image\2022-10-19 15-07-52 60°.bmp"
	path3 = r"D:\迅雷下载\偏振光\偏振光仿真和上位机\Image\2022-10-19 15-07-52 120°.bmp"

	MainWindow = Target_view(dir1=path1, dir2=path2, dir3=path3, SAVE_RESULT='不保存')
	MainWindow.show()
	exit(app.exec_())
